package com.yongbao.utils;

import org.springframework.data.domain.Sort;

/**
 * @author: zyh
 * @date: 2023-06-25
 */
public class PageUtils {
    public static String getOrderBy(Sort sort) {
        if (sort == null) {
            return null;
        }
        Sort.NullHandling nullHandling;
        StringBuilder orderBuilder = new StringBuilder();
        for (Sort.Order order : sort) {
            orderBuilder.append(order.getProperty()).append(order.isAscending() ? " ASC" : " DESC");
            nullHandling = order.getNullHandling();
            if (nullHandling != null) {
                if (Sort.NullHandling.NULLS_FIRST == nullHandling) {
                    orderBuilder.append(" NULLS FIRST");
                } else if (Sort.NullHandling.NULLS_LAST == nullHandling) {
                    orderBuilder.append(" NULLS LAST");
                }
            }
            orderBuilder.append(",");
        }
        return orderBuilder.delete(orderBuilder.length() - 1, orderBuilder.length()).toString();
    }
}
